<?php

defined('_JEXEC') or die();

jimport( 'joomla.application.component.controller' ); 

class ApkipasControllerApkipas extends JController 
{
	
	protected $option;
	protected $controller;
	
	function __construct( $default = array()){
		$this->option = 'com_apkipas';
		$this->controller = 'apkipas';
		$this->layout = JRequest::getVar('layout') ;
		parent::__construct( $default );
	}

	function display()
	{
		switch($this->getTask()){
			case 'add':{
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view'  , $this->controller);
			} 
			break;
			default:
				JRequest::setVar( 'view'  , $this->controller);
				break;
		}
		parent::display();
	}
	
	function save(){
		$user = JFactory::getUser();
		$post = JRequest::get('post');
		$model = $this->getModel('apkipas');
		$post['alias'] = ApkipasHelper::setAlias($post['title'],$post['alias']);
		
		if($post['id'] == 0){
			$post['created_date'] = date('Y-m-d H:i:s') ;
			$post['author'] = $user->id;
		}else{
			$post['modified_date'] = date('Y-m-d H:i:s') ;
			$post['modified'] = $user->id;
		}
		//save
		$id = $model->save($post);
		
		if($id != 0){
			$this->saveTag($post['tags'],$id);
			$this->setRedirect('index.php?option='.$this->option.'&view='.$this->controller,'Saved');
		}
		else{
			$this->setRedirect('index.php?option='.$this->option.'&view='.$this->controller,'Error');
		}
	}
	
	function saveTag($tag,$id){
		$db = &JFactory::getDbo();
		if($tag == '')
			return false;
		
		$tag_arr = explode(',',$tag);
		if(count($tag_arr) > 0){
			$tag = $this->getModel('tags');
			foreach ($tag_arr as $val){
				
				//save tag table
				$tagpost['text']=trim($val);
				$tagpost['id'] = null;
				$tagid = $tag->save($tagpost);

				$query = "INSERT INTO #__apkipas_refs (`id`, `tagid`) VALUES ({intval($id)}, {intval($tagid)})";
				$db->setQuery($query);
				$db->query();
			}
		}
	}
	
	function updateCat(){
		$db = JFactory::getDbo();
		$query = 'select a.id,a.cat_id,a.product_id,b.requirements , b.category
		FROM apkipas_apkipas_titles as a
		left join apkipas_apkipas_resources as b on a.product_id = b.id';
		
		$db->setQuery($query);
		$rs = $db->loadObjectList();
		foreach ($rs as $item){
			
			if($item->category == 'Games'){
				if(strpos($item->requirements, 'Compatible with iPad') === false)
					$newcat = 10;
				else 
					$newcat = 11;
			}
			else{
				if(strpos($item->requirements, 'Compatible with iPad') === false)
					$newcat = 12;
				else 
					$newcat = 13;
			}
			
			if($newcat != $item->cat_id){
				$newupdate = 'UPDATE apkipas_apkipas_titles SET cat_id = '.$newcat.' WHERE id = '.$item->id;
				$db->setQuery($newupdate);
				$db->query();
			}
		}
	}

	function make_apkipas_tags(){
		$db = JFactory::getDbo();
		$query = 'select a.alias,b.itune_id,b.seller , b.category
		FROM apkipas_apkipas_titles as a
		left join apkipas_apkipas_resources as b on a.product_id = b.id';
		$db->setQuery($query);
		$rs = $db->loadObjectList();
		foreach($rs as $tags){
			$arr_alias = explode('-', $tags->alias);
			unset($arr_alias[count($arr_alias) - 1]);
			$arr_alias[] = $tags->category;
			$arr_alias[] = $tags->seller;
				
			foreach ($arr_alias as $v=>$value)
			{
				if(strlen($value)>= 4){
					$query = 'SELECT id FROM #__apkipas_tags WHERE `text` = "'.$value.'"';
					$db->setQuery($query);
					$rs = $db->loadObject();
			
					if(intval($rs->id) != 0){
						//already tags
						//insert ref
						$obj = new stdClass();
						$obj->id = $tags->itune_id;
						$obj->tagid = $rs->id;
			
						$db->insertObject('#__apkipas_refs', $obj);
						unset($obj);
					}
					else{
						//insert tags
						$obj = new stdClass();
						$obj->id = null;
						$obj->text = $value;
						$obj->alias = JApplication::stringURLSafe($value);
						
						$db->insertObject('#__apkipas_tags', $obj);
						$tagid = $db->insertid();
						unset($obj);
			
						$obj = new stdClass();
						$obj->id = $tags->itune_id;
						$obj->tagid = $tagid;
			
						$db->insertObject('#__apkipas_refs', $obj);
						unset($obj);
					}
				}
			}
		}
		
	}
	
	function sellerTag(){
		$db = JFactory::getDbo();
		$query = 'select seller FROM apkipas_apkipas_resources ';
		$db->setQuery($query);
		$rs = $db->loadObjectList();
			
		foreach ($rs as $v=>$value)
		{
			$query = 'UPDATE #__apkipas_tags SET type = 2 WHERE `text` = "'.$value->seller.'"';
			$db->setQuery($query);
			$db->query();
		}
	}
	
	function convertUtf8(){
		$id = JRequest::getVar('id');
		$db = JFactory::getDbo();
		$query = 'SELECT * FROM apkipas_apkipas_resources';
		$db->setQuery($query);
		$objs = $db->loadObjectList();
		
		foreach($objs as $obj){
			$tmpobj = new stdClass();
			$tmpobj->id = $obj->id;
			$tmpobj->title =$obj->title;
			$tmpobj->itune_id = $obj->itune_id;
			$tmpobj->android_id= $obj->android_id;
			$tmpobj->des=utf8_encode($obj->des);
			$tmpobj->meta_des=$obj->meta_des;
			$tmpobj->category=$obj->category;
			$tmpobj->updated=$obj->updated;
			$tmpobj->version=$obj->version;
			$tmpobj->size=$obj->size;
			$tmpobj->language=$obj->language;
			$tmpobj->seller=$obj->seller;
			$tmpobj->requirements=$obj->requirements;
			$tmpobj->bg=$obj->bg;
			$tmpobj->p1=$obj->p1;
			$tmpobj->p2=$obj->p2;
			$tmpobj->p3=$obj->p3;
			$tmpobj->p4=$obj->p4;
			$tmpobj->p5=$obj->p5;
			
			if($db->updateObject('apkipas_apkipas_resources', $tmpobj, 'id') != false)
				echo $obj->id.'<br>'; 
		}
	}
}
